<?php

class Model_Tagged extends Model
{
	public function get_data()
	{	
		$link=$this->connect_to_db();
		$taglist_q = mysqli_query($link, 
			"select tag, COUNT(*) as count 
			from photo_tags 
			group by tag order by count desc;");		

		$taglistarray = array();
		while($taglist = mysqli_fetch_array($taglist_q,  MYSQLI_ASSOC))
			array_push($taglistarray, 
				array(
					'Tag' => $taglist['tag'],
					'Count' => $taglist['count']
					)
				);
		mysqli_close($link);
		return $taglistarray;

	}
	public function get_tag_data()
	{
		if(isset($_GET["tag"]))
		{
			$gettagdata = array();
			$tag=$_GET["tag"];

		$getphotos = $this->getAll("
					select photos_list.id,date_added,page_number,photo_filename 
					from photos_list 
					join photo_tags on photos_list.id=photo_tags.photo_id 
					where photo_tags.tag= ?
					order by photos_list.id desc;", $tag);

		//formatting data
		foreach($getphotos as $row)
		{
			array_push($gettagdata, 
				array(
					'Id' => $row['id'],
					'Date' => $row['date_added'],
					'Page' => $row['page_number'],
					'Photo' => $row['photo_filename']
					)
				);
		}

		return array($gettagdata, $tag);
		}
	}

}
